package com.milkshopbackend.milkshop.controller;

import com.milkshopbackend.milkshop.Service.CartSelectionService;
import com.milkshopbackend.milkshop.common.Result;
import com.milkshopbackend.milkshop.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cart/selection")
public class CartSelectionController {

    @Autowired
    private CartSelectionService cartSelectionService;

    @PostMapping("/select/{cartId}")
    public Result<Void> selectCart(@PathVariable Long cartId) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            cartSelectionService.selectCart(userId, cartId);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "Failed to select cart: " + e.getMessage());
        }
    }

    @PostMapping("/unselect/{cartId}")
    public Result<Void> unselectCart(@PathVariable Long cartId) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            cartSelectionService.unselectCart(userId, cartId);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "Failed to unselect cart: " + e.getMessage());
        }
    }

    @PostMapping("/select-all")
    public Result<Void> selectAllCarts() {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            cartSelectionService.selectAllCarts(userId);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "Failed to select all carts: " + e.getMessage());
        }
    }

    @PostMapping("/unselect-all")
    public Result<Void> unselectAllCarts() {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            cartSelectionService.unselectAllCarts(userId);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "Failed to unselect all carts: " + e.getMessage());
        }
    }

    @GetMapping("/selected")
    public Result<List<Long>> getSelectedCartIds() {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            List<Long> selectedCartIds = cartSelectionService.getSelectedCartIds(userId);
            return Result.success(selectedCartIds);
        } catch (Exception e) {
            return Result.error(500, "Failed to get selected cart IDs: " + e.getMessage());
        }
    }
} 